package com.daiwoniu.demo;

import com.daiwoniu.base.util.CaptchaUtils;
import com.daiwoniu.base.util.Utils;
import com.daiwoniu.base.web.BaseController;
import com.daiwoniu.base.web.auth.UserContext;
import com.daiwoniu.demo.admin.user.entity.User;
import com.daiwoniu.demo.admin.user.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.inject.Inject;

@Controller
@RequestMapping("/")
public class IndexController extends BaseController {
	@Inject
	private UserService userService;

	public static class LoginForm {
		private String name;
		private String password;
		private String code;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}
	}

	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model) {
		return "index";
	}

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String toLogin(Model model) {
		return "login";
	}

	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(Model model) {
		getRequest().getSession().invalidate();
		return "redirect:/";
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(LoginForm loginForm, Model model) {
		if(Utils.isBlankEmpty(loginForm.getName())){
			model.addAttribute("errorMsg", "没写登录名");
			return "login";
		}
		if(Utils.isBlankEmpty(loginForm.getPassword())){
			model.addAttribute("name", loginForm.getName());
			model.addAttribute("errorMsg", "没写密码");
			return "login";
		}
		User user = userService.login(loginForm.getName(),
				loginForm.getPassword());
		if (user == null) {
			model.addAttribute("name", loginForm.getName());
			model.addAttribute("errorMsg", "用户名密码不匹配");
			return "login";
		}
		getRequest().getSession().setAttribute(UserContext.USER_ID_SESSION_ATTRIBUTE, user.getId());
		return "redirect:/";
	}

	@RequestMapping(value = "register", method = RequestMethod.GET)
	public String toRegister(Model model) {
		return "register";
	}

	@RequestMapping(value = "register", method = RequestMethod.POST)
	public String register(RegisterForm registerForm, Model model) {

		if(!validRegister(registerForm, model)){
			model.addAttribute("registerForm", registerForm);
			return "register";
		}
		User userdb = userService.findByLoginName(registerForm.getLoginName());
		if(userdb != null){
			model.addAttribute("errorMsg", "登录名重复，换个吧");
			model.addAttribute("registerForm", registerForm);
			return "register";
		}

		// 注册用户
		User user = new User();
		user.setName(registerForm.getName());
		user.setLoginName(registerForm.getLoginName());
		user.setLoginPassword(registerForm.getLoginPassword());
		user.setUser_type("1");
		userService.create(user, null);

		model.addAttribute("successMsg", "注册成功，请登录！");
		model.addAttribute("name", registerForm.getLoginName());
		return "login";
	}

	private boolean validRegister(RegisterForm registerForm, Model model){
		if(Utils.isBlankEmpty(registerForm.getName())){
			model.addAttribute("errorMsg", "没写昵称啊");
			return false;
		}
		if(Utils.isBlankEmpty(registerForm.getLoginName())){
			model.addAttribute("errorMsg", "没写登录名啊");
			return false;
		}
		if(Utils.isBlankEmpty(registerForm.getLoginPassword())){
			model.addAttribute("errorMsg", "没写密码啊");
			return false;
		}
		if(Utils.isBlankEmpty(registerForm.getConfirmPassword())){
			model.addAttribute("errorMsg", "没写确认密码啊");
			return false;
		}
		if(!registerForm.getLoginPassword().equalsIgnoreCase(registerForm.getConfirmPassword())){
			model.addAttribute("errorMsg", "两次密码不一样啊");
			return false;
		}
		if(Utils.isBlankEmpty(registerForm.getVerificationCode())){
			model.addAttribute("errorMsg", "没写验证码啊");
			return false;
		}
		if(registerForm.getName().length() > 20){
			model.addAttribute("errorMsg", "昵称太长了啊");
			return false;
		}
		if(registerForm.getLoginName().length() > 20){
			model.addAttribute("errorMsg", "登录名太长了啊");
			return false;
		}
		if(registerForm.getLoginPassword().length() > 30){
			model.addAttribute("errorMsg", "密码太长了啊");
			return false;
		}
		String code = Utils.toString(getRequest().getSession().getAttribute(CaptchaUtils.CAPTCHA_SESSION_KEY));
		if(!registerForm.getVerificationCode().equalsIgnoreCase(code)) {
			model.addAttribute("errorMsg", "验证码错误");
			return false;
		}
		return true;
	}


	public static class RegisterForm {
		private String name;
		private String loginName;
		private String verificationCode;
		private String loginPassword;
		private String confirmPassword;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getLoginName() {
			return loginName;
		}

		public void setLoginName(String loginName) {
			this.loginName = loginName;
		}

		public String getVerificationCode() {
			return verificationCode;
		}

		public void setVerificationCode(String verificationCode) {
			this.verificationCode = verificationCode;
		}

		public String getLoginPassword() {
			return loginPassword;
		}

		public void setLoginPassword(String loginPassword) {
			this.loginPassword = loginPassword;
		}

		public String getConfirmPassword() {
			return confirmPassword;
		}

		public void setConfirmPassword(String confirmPassword) {
			this.confirmPassword = confirmPassword;
		}
	}
}